﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CoreData;
using ActionServices;
using Helper;

namespace ZidoniCMS.Areas.Administrator.Controllers
{
    public class MerchantNotifyManagementController : BaseController
    {
        //
        // GET: /Administrator/Category/

        public MerchantNotifyManagementController(IMerchant_NotifyActionService merchantnotify)
            : base(merchantnotify)
        {
            try
            {
                if(Permission==null ||Permission.Count<=0)
                {
                    SetPermission(UserID, GroupID, UserName);
                }
                SessionManagement.SetSesionValue("pvalue", Permission.FirstOrDefault(c => c.Module.Equals(Helper.ValueDefine.CATEGORY)).Role);
            }
            catch { }
        }

        #region Main 

        public ActionResult Index()
        {
            return SetUpSecurity();
        }

        public ActionResult _ShowAll()
        {
            var data = MerchantNotifyService.GetList();
            return PartialView(data);
        }

        #endregion

        #region CRUD
         /// <summary>
         /// view
         /// </summary>
         /// <returns></returns>
        public ActionResult AddOrUpdate(int id=0)
        {
            return CanUpdate((int)id);
        }

        public ActionResult _AddOrUpdate(int id)
        {
            var data = MerchantNotifyService.GetByID(id);
            if (data == null)
                data = new Merchant_Notify();
            data.ListMerchantExt = MerchantNotifyService.GetListMerchant(data.MerchantID);
            return PartialView(data);
        }

        /// <summary>
        /// Action
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult AddOrUpdate(Merchant_Notify model, HttpPostedFileBase file)
        {
            foreach(var item in model.MerchantIDExt)
            {
                model.MerchantID += item + ",";
            }
            if (model.Active == true)
                model.Status = 1;
            else { model.Status = 0; }
           // if (model.ID == 0)
                model.Sender = SessionManagement.GetSessionReturnToString("UserName");
            model.MerchantID = model.MerchantID.Remove(model.MerchantID.Length - 1, 1);
            var id = this.MerchantNotifyService.Save(model);
            PathUpload = "";
            var data = this.MerchantNotifyService.GetByID(id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }


        public ActionResult Delete(int id) 
        {
            this.MerchantNotifyService.Delete(this.MerchantNotifyService.GetByID(id));
            return RedirectToAction("Index", "MerchantNotifyManagement");
        }

        #endregion

    }
}
